package com.mobialia.socialmediademo;


import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UpdateAdapter extends BaseAdapter {

	static final String TAG = "UpdateAdapter";
	
	private Context mContext;
	ArrayList<Update> updates;	
	ImageCache imageCache;

	public UpdateAdapter(Context c) {
		mContext = c;
		imageCache = ImageCache.getInstance();
	}

	public int getCount() {
		if (updates == null) return 0;
		return updates.size();
	}

	public Update getObject(int position) {
		if (updates == null) return null;
		return updates.get(position);
	}
	
	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout ll = (LinearLayout) LayoutInflater.from(mContext).inflate(
				R.layout.update_adapter, parent, false);
		
		try {
			Update update = getObject(position);
		
			final ImageView imageView = (ImageView) ll.findViewById(R.id.Image);
			imageCache.myImageLoad(mContext, imageView, update.getImageUrl());
			
			TextView text = (TextView) ll.findViewById(R.id.Text);
			text.setText(Html.fromHtml(update.getMessage()));
		
			TextView userId = (TextView) ll.findViewById(R.id.UserId);
			userId.setText(update.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ll;
	}
	
	public void setUpdates(ArrayList<Update> updates) {
		this.updates = updates;
	}
}
